<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="主体名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'ztname', validatorRules.ztname]" placeholder="请输入主体名称"></a-input>
        </a-form-item>
          
        <a-form-item label="法人代表姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'frname', validatorRules.frname]" placeholder="请输入法人代表姓名"></a-input>
        </a-form-item>
          
        <a-form-item label="经营地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'address', validatorRules.address]" placeholder="请输入经营地址"></a-input>
        </a-form-item>
          
        <a-form-item label="企业类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-multi-select-tag type="list_multi" v-decorator="['companyType']" :trigger-change="true" dictCode="company_type" placeholder="请选择企业类型"/>
        </a-form-item>
          
        <a-form-item label="员工人数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'memberNumber', validatorRules.memberNumber]" placeholder="请输入员工人数"></a-input>
        </a-form-item>
          
        <a-form-item label="带贫人数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'helpNumber', validatorRules.helpNumber]" placeholder="请输入带贫人数"></a-input>
        </a-form-item>
          
        <a-form-item label="批次" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'batch', validatorRules.batch]" placeholder="请输入批次"></a-input>
        </a-form-item>
          
        <a-form-item label="主营" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['mainBusiness']" rows="4" placeholder="请输入主营"/>
        </a-form-item>
          
        <a-form-item label="借款用途" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['borrowingPurposes']" rows="4" placeholder="请输入借款用途"/>
        </a-form-item>
          
        
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JMultiSelectTag from "@/components/dict/JMultiSelectTag"
  
  export default {
    name: "LoanRecommendationWithPoorEnterprisesModal",
    components: { 
      JMultiSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        ztname:{},
        frname:{},
        address:{},
        companyType:{},
        memberNumber:{},
        helpNumber:{},
        batch:{},
        mainBusiness:{},
        borrowingPurposes:{},
        },
        url: {
          add: "/loanmanagement/loanRecommendationWithPoorEnterprises/add",
          edit: "/loanmanagement/loanRecommendationWithPoorEnterprises/edit",
        }
     
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'ztname','frname','address','companyType','memberNumber','helpNumber','batch','mainBusiness','borrowingPurposes'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'ztname','frname','address','companyType','memberNumber','helpNumber','batch','mainBusiness','borrowingPurposes'))
      }
      
    }
  }
</script>